<?php

namespace App\Lib;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use Illuminate\Support\Str;

class XiaoHongSdk
{
    public $cookie;

    public $client;
    public function __construct($cookie){

        $this->cookie = $cookie;
        $this->client = new Client();
    }

    //获取小红书的token
    public function getTopicList($key){

        $headers = [
            'Referer' => 'https://creator.xiaohongshu.com/',
            'Origin' => 'https://creator.xiaohongshu.com/',
            'Cookie' => $this->cookie,
            'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
        ];

        $options = [
            'json' => [
                'keyword' => $key,
                 'page' => [
                     'page_size' => 20,
                     'page' => 1
                 ]
            ],
            "verify" => false
        ];

        $request = new Request('POST', 'https://edith.xiaohongshu.com/web_api/sns/v1/search/topic', $headers);
        $res = $this->client->sendAsync($request, $options)->wait();
        return json_decode($res->getBody(), true);

    }


    public function getMillisecond() {
        list($usec, $sec) = explode(" ", microtime());
        $time = intval(((float)$usec + (float)$sec) * 1000);
        return $time;
    }
}
